package top.mcwebsite.playandroid.business.settings.impl.ui

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.ArrowBack
import androidx.compose.material.icons.rounded.ChevronRight
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import top.mcwebsite.lib.route.api.EasyRouter
import top.mcwebsite.lib.router.annotation.Route
import top.mcwebsite.playandroid.business.settings.impl.vm.SettingsIntent
import top.mcwebsite.playandroid.business.settings.impl.vm.SettingsUiEffect
import top.mcwebsite.playandroid.business.settings.impl.vm.SettingsUiState
import top.mcwebsite.playandroid.business.settings.impl.vm.SettingsViewModel
import top.mcwebsite.playandroid.common.constant.RouteConfig
import top.mcwebsite.playandroid.strings.Str

@Route(RouteConfig.Settings.PATH)
@Composable
fun SettingsPage() {
    val viewModel: SettingsViewModel = viewModel()
    val uiState by viewModel.uiStateFlow.collectAsState()
    val snackBarState = remember {
        SnackbarHostState()
    }
    viewModel.HandleUiEffect {
        when (it) {
            is SettingsUiEffect.NavigationBackUiEffect -> {
                EasyRouter.popBackStack()
            }

            is SettingsUiEffect.ShowSnackMessageUiEffect -> {
                snackBarState.showSnackbar(it.snackMsg)
            }

            is SettingsUiEffect.NavigatePrivacyPageUiEffect -> {
                // 待实现
            }
        }
    }
    SettingsPage(uiState, snackBarState) {
        viewModel.sendIntent(it)
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun SettingsPage(
    uiState: SettingsUiState,
    snackBarState: SnackbarHostState,
    sendIntent: (SettingsIntent) -> Unit,
) {
    Scaffold(
        topBar = {
            TopAppBar(
                navigationIcon = {
                    IconButton(onClick = { sendIntent(SettingsIntent.ClickBackIntent) }) {
                        Icon(imageVector = Icons.AutoMirrored.Rounded.ArrowBack, contentDescription = stringResource(id = Str.back))
                    }
                },
                title = {
                    Text(text = stringResource(id = Str.settings))
                },
            )
        },
        snackbarHost = {
            Box(modifier = Modifier.fillMaxHeight()) {
                SnackbarHost(hostState = snackBarState, modifier = Modifier.align(Alignment.Center))
            }
        }
    ) {
        Box(modifier = Modifier.padding(it)) {
            SettingsPageContent(uiState, sendIntent)
            LogoutDialog(
                isShow = uiState.isShowLogoutDialog,
                confirm = {
                    sendIntent(SettingsIntent.LogoutConfirmIntent)
                },
                cancel = {
                    sendIntent(SettingsIntent.LogoutCancelIntent)
                }
            )
        }
    }
}

@Composable
private fun SettingsPageContent(
    uiState: SettingsUiState,
    sendIntent: (SettingsIntent) -> Unit
) {
    Column(
        modifier = Modifier
    ) {
        Row(
            modifier = Modifier
                .padding(vertical = 10.dp, horizontal = 16.dp)
        ) {
            Text(
                text = stringResource(id = Str.account_manager),
            )
            Spacer(modifier = Modifier.weight(1f))
            Icon(imageVector = Icons.Rounded.ChevronRight, contentDescription = null)
        }
        Row(
            modifier = Modifier.padding(vertical = 10.dp, horizontal = 16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Column {
                Text(text = stringResource(id = Str.follow_system_mode))
                Text(
                    text = stringResource(id = Str.follow_system_mode_tips),
                    style = MaterialTheme.typography.labelMedium,
                    color = LocalContentColor.current.copy(alpha = 0.7f)
                )
            }
            Spacer(modifier = Modifier.weight(1f))
            Switch(checked = uiState.isFollowSystemTheme, onCheckedChange = {
                sendIntent(SettingsIntent.ToggleFollowSystemThemeIntent)
            })
        }
        Row(
            modifier = Modifier
                .padding(vertical = 10.dp, horizontal = 16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(text = stringResource(id = Str.dark_mode))
            Spacer(modifier = Modifier.weight(1f))
            Switch(checked = uiState.isDarkMode, onCheckedChange = {
                sendIntent(SettingsIntent.ToggleDarkModeIntent)
            })
        }
        Row(modifier = Modifier.padding(vertical = 10.dp, horizontal = 16.dp)) {
            Text(
                text = stringResource(id = Str.version_update),
            )
            Spacer(modifier = Modifier.weight(1f))
            Icon(imageVector = Icons.Rounded.ChevronRight, contentDescription = null)
        }
        Row(
            modifier = Modifier
                .clickable {
                    sendIntent(SettingsIntent.ClickAboutUsIntent)
                }
                .padding(vertical = 10.dp, horizontal = 16.dp)
        ) {
            Text(
                text = stringResource(id = Str.about_us),
            )
            Spacer(modifier = Modifier.weight(1f))
            Icon(imageVector = Icons.Rounded.ChevronRight, contentDescription = null)
        }
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .clickable {
                    sendIntent(SettingsIntent.ClickPrivacyIntent)
                }
        ) {
            Text(
                text = stringResource(id = Str.privacy),
                modifier = Modifier.padding(horizontal = 16.dp, vertical = 10.dp)
            )
        }
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .clickable {
                    sendIntent(SettingsIntent.ClickLogoutIntent)
                }
        ) {
            Text(
                text = stringResource(id = Str.logout),
                color = Color.Red,
                modifier = Modifier
                    .padding(vertical = 10.dp, horizontal = 16.dp)
            )
        }
    }
}